/**
 * 正则模版替换
 *
 * 1.渲染对象：var html = template('tmplId', {key: value})
 * 2.渲染列表：var html = template('tmplId', [{key: value},{key: value}])
 * 3.回显结果：document.getElementById('xx').innerHTML = html;
 *
 * @param  {String} tmplId [模版ID]
 * @param  {Object} data   [数据]
 * @return {String}        [HTMl] 替换结束之后的HTML代码
 */
function template(tmplId, data) {
    var template = document.getElementById(tmplId).innerHTML;

    //- 正则替换
    var replace = function(obj) {
        var t, key, reg;
        for (key in obj) {
            reg = new RegExp('{{' + key + '}}', 'ig');
            t = (t || template).replace(reg, obj[key]);
        }
        return t;
    };

    //- 判断是否为空对象 {}
    var isEmptyObject = function(e) {
        var t;
        for (t in e) return !1;
        return !0;
    };

    var result = '';
    if (!!data) {
        var len = data.length;
        if (!!len) {
            if (0 === len)
                result = template;

            //- 循环数组
            for (var i = 0; i < len; i++) {
                result += replace(data[i]);
            }

        } else {
            result = isEmptyObject(data) ? template : replace(data);
        }
    } else {
        result = template;
    }

    return result;
}